//implicit def stringToInt(s:String) = new Integer(s)

import scala.io.Source
import scala.runtime.RichString
import scala.runtime.RichLong
import scala.actors._
import scala.actors.Actor._

class Injector extends Actor {

        def elapsedTime(start : Long) : String = {
                new RichLong(System.currentTimeMillis - start).toString
        }


	def fetch(src:String) : Unit = {
	        val start = System.currentTimeMillis
	        Source.fromFile(src)
	        println(elapsedTime(start))
	}

	def act() {
		react {
			case (src: String) =>
				fetch(src)
				act()
		}
	}
}

object ScalaMeter {
	val appname = "scalameter"
	val nbCore = 2

	def prepareActors() = {
		var injectors = new Array[Injector](nbCore)
		(0 to nbCore).foreach (
			injectors update (_ ,new Injector)
		)
		injectors
	}

	def injectorIncr(x:Int,list: Array[Injector]) = x+1

	def main(args:Array[String]) {
		println(appname + ":" + args(0))
		val injectors = prepareActors()
		val vUsers = new RichString(args(1)).toInt
		// Partially applied function !
/*
			val nextId = currentId + 1;
			if ( nextId >= list.size ) 0;
			else nextId;
		};
*/
		var currentId = 0;
//		(0 to vUsers).foreach{ x => currentId = injectorIncr(currentId,injectors) }
		(0 to vUsers).foreach{ x => currentId = injectorIncr(currentId,injectors) }
	}
}

